package com.xyw.code.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xyw.code.constant.XConstant;
import com.xyw.code.dto.UserDTO;
import com.xyw.code.dto.UserInfoDTO;
import com.xyw.code.entity.SysDept;
import com.xyw.code.entity.SysRole;
import com.xyw.code.entity.SysUser;
import com.xyw.code.log.SysLog;
import com.xyw.code.security.XUser;
import com.xyw.code.security.util.JwtUtil;
import com.xyw.code.security.util.SecurityUtil;
import com.xyw.code.service.ISysDeptService;
import com.xyw.code.service.ISysRoleService;
import com.xyw.code.service.ISysUserService;
import com.xyw.code.utils.EmailUtil;
import com.xyw.code.utils.Result;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author xuyiwei
 * @since 2019-07-30
 */
@RestController
@RequestMapping("/user")
public class SysUserController {

    @Autowired
    private ISysUserService userService;

    @Autowired
    private EmailUtil emailUtil;

    /**
     * 获取用户列表集合
     *
     * @param userDTO
     * @return
     */
    @GetMapping("/list")
    @PreAuthorize("hasAuthority('sys:user:view')")
    public Result<IPage<SysUser>> getList( UserDTO userDTO) {
        return Result.ok(userService.getUsersWithRolePage(userDTO));
    }

    /**
     * 保存用户包括角色和部门
     *
     * @param userDto
     * @return
     */
    @SysLog(descrption = "新增用户")
    @PostMapping("/add")
    @PreAuthorize("hasAuthority('sys:user:add')")
    public Result<Boolean> add(@RequestBody UserDTO userDto) {
        return Result.ok(userService.insertUser(userDto));
    }


    /**
     * 更新用户包括角色和部门
     *
     * @param userDto
     * @return
     */
    @SysLog(descrption = "更新用户")
    @PostMapping("/update")
    @PreAuthorize("hasAuthority('sys:user:update')")
    public Result<Boolean> update(@RequestBody UserDTO userDto) {
        return Result.ok(userService.updateUser(userDto));
    }


    /**
     * 删除用户包括角色和部门
     *
     * @param userId
     * @return
     */
    @SysLog(descrption = "删除用户")
    @PostMapping("/delete/{userId}")
    @PreAuthorize("hasAuthority('sys:user:delete')")
    public Result<Boolean> delete(@PathVariable("userId") Integer userId) {
        return Result.ok(userService.removeUser(userId));
    }



    /**
     * 重置密码
     *
     * @param userId
     * @return
     */
    @SysLog(descrption = "充值密码")
    @PostMapping("/restPass/{userId}")
    @PreAuthorize("hasAuthority('sys:user:rest')")
    public Result<Boolean> restPass(@PathVariable("userId") Integer userId) {
        return Result.ok(userService.restPass(userId));
    }


    /**
     * 上传用户头像
     * @param file
     * @return
     */
    @PostMapping("/uploadAvatar")
    public Result<Boolean> uploadAvatar(@RequestParam("file")MultipartFile file) {
        return Result.ok(userService.uploadAvatar(file));
    }

    /**
     * 更新用户包括角色和部门
     *
     * @param userDto
     * @return
     */
    @SysLog(descrption = "更新用户详情")
    @PostMapping("/updateUserInfo")
    public Result<Boolean> updateUserInfo(@RequestBody UserInfoDTO userDto) {
        return Result.ok(userService.updateUserInfo(userDto));
    }

    /**
     * 更新用户包括角色和部门
     ** @return
     */
    @SysLog(descrption = "修改密码")
    @PostMapping("/updatePass")
    @PreAuthorize("hasAuthority('sys:user:updatePass')")
    public Result<Boolean> updatePass(@RequestParam String oldPass, String newPass) {
        return Result.ok(userService.updatePass(oldPass,newPass));
    }

    /**
     * 发送email code
     ** @return
     */
    @SysLog(descrption = "发送邮件验证码")
    @PostMapping("/sendMailCode/{email}")
    public Result<Boolean> sendMailCode(@PathVariable String email,HttpServletRequest request) {
        emailUtil.sendSimpleMail(email,request);
        return Result.ok(true);
    }



    /**
     * 更改邮箱
     ** @return
     */
    @SysLog(descrption = "发送邮件验证码")
    @PostMapping("/updateEmail")
    public Result<Boolean> updateEmail(@RequestParam String email,@RequestParam String code,HttpServletRequest request) {
        String sessionCode = (String) request.getSession().getAttribute(XConstant.RESET_MAIL );
        return Result.ok(userService.updateEmail(email,code,sessionCode));
    }

}

